package net.diaowen.dwsurvey.controller;

import net.diaowen.dwsurvey.service.FindDictManager;
import net.diaowen.dwsurvey.service.ReportDescManager;
import net.diaowen.dwsurvey.service.TreportManager;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/showTable")
public class ShowTableController {
    //注入表头
    @Autowired
    private ReportDescManager reportDescManager;
    //注入内容
    @Autowired
    private TreportManager treportManager;

    @Autowired
    private FindDictManager findDictManager;

    @RequestMapping("/dynamictable.do")
    public ModelAndView dynamictable(HttpServletRequest request, HttpServletResponse response) throws Exception{
            ModelAndView modelAndView = new ModelAndView();
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/json; charset=utf-8");
            String reportdescid = request.getParameter("reportdescid");
            String orgid = request.getParameter("orgid");
            String civilyear = request.getParameter("civilyear");
            modelAndView.setViewName("/diaowen-table/dynamictable");

            modelAndView.addObject("reportdescid", reportdescid);
            modelAndView.addObject("orgid",orgid);
            modelAndView.addObject("civilyear",civilyear);
            System.out.println("进入dynamictable.do属性");
            return modelAndView;
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelAndView;
    }

    //显示动态表单
    @RequestMapping("/table.do")
    public ModelAndView table(HttpServletRequest request, HttpServletResponse response) throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/json; charset=utf-8");
            String reportdescid = request.getParameter("reportdescid");
            String orgid = request.getParameter("orgid");
            String civilyear = request.getParameter("civilyear");
            modelAndView.setViewName("/diaowen-table/ShowDynamicTables");

            modelAndView.addObject("reportdescid", reportdescid);
            modelAndView.addObject("orgid",orgid);
            modelAndView.addObject("civilyear",civilyear);
            System.out.println("进入table.do");
            return modelAndView;
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelAndView;
    }

    //查询所有的表头（标题）
    @RequestMapping("/findtabletitles.do")
    @ResponseBody
    public void findtabletitle(HttpServletRequest request,HttpServletResponse response) throws Exception{
        try{
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/json; charset=utf-8");
            String id=request.getParameter("id");
            System.out.println("进入findtabletitle");
            String data=reportDescManager.findtabletitles(id);
            response.getWriter().write(data);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //查询所有table的内容
    @RequestMapping("/findtablevalues.do")
    @ResponseBody
    public void findtablevalue(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");
        String reportdescid =request.getParameter("reportdescid");
        String civilyear = request.getParameter("civilyear");
        String orgid=request.getParameter("orgid");
        try{
            String data=treportManager.findtablevalue(reportdescid,civilyear,orgid);
            response.getWriter().write(data);
        }
        catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("data");
        }

    }

    //根据dict_id查询该字典所有结果
    @RequestMapping("/findtabledict.do")
    @ResponseBody
    public void findtabledict(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");
        String dictid=request.getParameter("dictid");
        try{
            List<Map<String,String>> list=findDictManager.finddict(dictid);
            JSONArray jsonArray=new JSONArray(list);
            System.out.println("1111111111");
            response.getWriter().write(jsonArray.toString());
        }
        catch (Exception e){
            System.out.println("111111111");
            e.printStackTrace();
        }

    }
    //根据dict_id查询该字典所有结果
    @RequestMapping("/updatetablevalue.do")
    @ResponseBody
    public void updatetablevalue(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");
//        String value=request.getParameter("value");
        String value=StringEscapeUtils.unescapeHtml(request.getParameter("value"));
        String reportdescid =request.getParameter("reportdescid");
        String civilyear = request.getParameter("civilyear");
        String orgid=request.getParameter("orgid");
        System.out.println(value);
        String s1="“";
        String s2="\"";
        String value1=value.replaceAll(s1,s2);
        System.out.println(value1);
        try{
            System.out.println("进入update操作");
            treportManager.updatetablevalue(value1,reportdescid,civilyear,orgid);
//            response.getWriter().write(data);
        }
        catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("data");
        }

    }
    //
    @RequestMapping("/showtablevalue.do")
    @ResponseBody
    public ModelAndView showtablevalue(HttpServletRequest request,HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/json; charset=utf-8");
            String reportdescid = request.getParameter("reportdescid");
            String orgid = request.getParameter("orgid");
            String civilyear = request.getParameter("civilyear");
            modelAndView.setViewName("/diaowen-table/ShowDynamicTables");

            modelAndView.addObject("reportdescid", reportdescid);
            modelAndView.addObject("orgid",orgid);
            modelAndView.addObject("civilyear",civilyear);
            System.out.println("进入ShowDynamicTables.do属性");
            return modelAndView;
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelAndView;
    }

}
